import Vue from 'vue'
import LoadingItem from '@/components/Loading'
// 使用extend创建子类（构造函数）
const LoadingConstructor = Vue.extend(LoadingItem)

let load = false // 避免重复调用
// 实例化loading
const instance = new LoadingConstructor({
  el: document.createElement('div'),
})

instance.show = false // 默认隐藏
// 默认接收参数
const option = {
  text: '鹏',
}
// 创建方法
const loading = {
  show(opt) {
    if (load) return
    load = true
    instance.show = true
    instance.option = Object.assign(option, opt)
    document.body.appendChild(instance.$el)
  },
  hide() {
    load = false
    instance.show = false
  },
}

export default {
  install() {
    if (!Vue.$loading) {
      Vue.$loading = loading
    }
    Vue.mixin({
      created() {
        this.$loading = Vue.$loading
      },
    })
  },
}
